package easy

func SortArrayByParity(nums []int) []int {
	n := len(nums)
	res := make([]int, n)
	left := 0
	right := n - 1
	for _, num := range nums {
		if num%2 == 0 {
			res[left] = num
			left++
		} else {
			res[right] = num
			right--
		}
	}
	return res
}

func SortArrayByParity1(nums []int) []int {
	n := len(nums)
	left := 0
	right := n - 1
	for left < right {
		for left < right && nums[left]%2 == 0 {
			left++
		}
		for left < right && nums[right]%2 != 0 {
			right--
		}
		if left < right {
			nums[left], nums[right] = nums[right], nums[left]
			left++
			right--
		}
	}
	return nums
}
